package com.ultrapower.ctool.xn_app;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.ultrapower.ctool.model.RegisterRolePirVO;
import com.ultrapower.ctool.picUtils.ActionSheetDialog;
import com.ultrapower.ctool.picUtils.PhotoUtil;
import com.ultrapower.ctool.picUtils.UriPathUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;

public class UploadPicActivity extends Activity implements View.OnClickListener{

    private Spinner pic_type;//图片类型
    private Spinner pic_ext_type;//拓展名类型
    private static final List<RegisterRolePirVO> types = new ArrayList<RegisterRolePirVO>();
    private static final List<RegisterRolePirVO> extteps = new ArrayList<RegisterRolePirVO>();
    static {
        types.add(new RegisterRolePirVO("", ""));
        types.add(new RegisterRolePirVO("1", "保存事件"));
        types.add(new RegisterRolePirVO("2", "处理事件"));
        types.add(new RegisterRolePirVO("3", "验收事件"));
        extteps.add(new RegisterRolePirVO("", ""));
        extteps.add(new RegisterRolePirVO("1", "JPG"));
        extteps.add(new RegisterRolePirVO("2", "PNG"));
        extteps.add(new RegisterRolePirVO("3", "其他"));
    }
    private ArrayAdapter<RegisterRolePirVO> typesAdapter;
    private ArrayAdapter<RegisterRolePirVO> exttepsAdapter;
    private AdapterView.OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
            ((TextView) parent.getChildAt(0)).setTextSize(18);
        }

        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
    private Button chose_pic_btn;//选择图片按钮
    private TextView upload_pic_btn;//上传
    private TextView back_btn;//返回
    private ImageView show_pic;//展示图片

    private static final String TAG = "UploadPicActivity";

    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_pic);
        init();
    }

    public void init(){
        pic_type = (Spinner) findViewById(R.id.pic_type);
        pic_ext_type = (Spinner) findViewById(R.id.pic_ext_type);
        chose_pic_btn = (Button) findViewById(R.id.chose_pic_btn);
        upload_pic_btn = (TextView) findViewById(R.id.upload_pic_btn);
        back_btn = (TextView) findViewById(R.id.back_btn);
        show_pic= (ImageView) findViewById(R.id.show_pic);
        chose_pic_btn.setOnClickListener(this);
        upload_pic_btn.setOnClickListener(this);
        back_btn.setOnClickListener(this);

        //将可选内容与ArrayAdapter连接起来
        typesAdapter = new ArrayAdapter<RegisterRolePirVO>(this, android.R.layout.simple_spinner_item, types);
        //设置下拉列表的风格
        typesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        pic_type.setAdapter(typesAdapter);
        //添加事件Spinner事件监听
        pic_type.setOnItemSelectedListener(OnCatSpinnerCL);
        //设置默认值
        pic_type.setVisibility(View.VISIBLE);

        //将可选内容与ArrayAdapter连接起来
        exttepsAdapter = new ArrayAdapter<RegisterRolePirVO>(this, android.R.layout.simple_spinner_item, extteps);
        //设置下拉列表的风格
        exttepsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        pic_ext_type.setAdapter(exttepsAdapter);
        //添加事件Spinner事件监听
        pic_ext_type.setOnItemSelectedListener(OnCatSpinnerCL);
        //设置默认值
        pic_ext_type.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.chose_pic_btn:
                options();
                break;
            case R.id.back_btn:
                finish();
                break;
            case R.id.upload_pic_btn:
                Log.i("PATH",path);
                break;
        }
    }

    protected void options() {
        ActionSheetDialog mDialog = new ActionSheetDialog(this).builder();
        mDialog.setTitle("选择");
        mDialog.setCancelable(false);
        mDialog.addSheetItem("拍照", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() {
            @Override
            public void onClick(int which) {
                PhotoUtil.photograph(UploadPicActivity.this);
            }
        }).addSheetItem("从相册选取", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() {
            @Override
            public void onClick(int which) {
                PhotoUtil.selectPictureFromAlbum(UploadPicActivity.this);
            }
        }).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == PhotoUtil.NONE)
            return;
        // 拍照
        if (requestCode == PhotoUtil.PHOTOGRAPH) {
            // 设置文件保存路径这里放在跟目录下
            File picture = null;
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                picture = new File(Environment.getExternalStorageDirectory() + PhotoUtil.imageName);
                if (!picture.exists()) {
                    picture = new File(Environment.getExternalStorageDirectory() + PhotoUtil.imageName);
                }
            } else {
                picture = new File(this.getFilesDir() + PhotoUtil.imageName);
                if (!picture.exists()) {
                    picture = new File(UploadPicActivity.this.getFilesDir() + PhotoUtil.imageName);
                }
            }

            path = PhotoUtil.getPath(this);// 生成一个地址用于存放剪辑后的图片
            if (TextUtils.isEmpty(path)) {
                Log.e(TAG, "随机生成的用于存放剪辑后的图片的地址失败");
                return;
            }
            Uri imageUri = UriPathUtils.getUri(this, path);
            PhotoUtil.startPhotoZoom(UploadPicActivity.this, Uri.fromFile(picture), PhotoUtil.PICTURE_HEIGHT, PhotoUtil.PICTURE_WIDTH, imageUri);
        }

        if (data == null)
            return;

        // 读取相册缩放图片
        if (requestCode == PhotoUtil.PHOTOZOOM) {

            path = PhotoUtil.getPath(this);// 生成一个地址用于存放剪辑后的图片
            if (TextUtils.isEmpty(path)) {
                Log.e(TAG, "随机生成的用于存放剪辑后的图片的地址失败");
                return;
            }
            Uri imageUri = UriPathUtils.getUri(this, path);
            PhotoUtil.startPhotoZoom(UploadPicActivity.this, data.getData(), PhotoUtil.PICTURE_HEIGHT, PhotoUtil.PICTURE_WIDTH, imageUri);
        }
        // 处理结果
        if (requestCode == PhotoUtil.PHOTORESOULT) {
            /**
             * 在这里处理剪辑结果，可以获取缩略图，获取剪辑图片的地址。得到这些信息可以选则用于上传图片等等操作
             * */

            /**
             * 如，根据path获取剪辑后的图片
             */
            Bitmap bitmap = PhotoUtil.convertToBitmap(path,PhotoUtil.PICTURE_HEIGHT, PhotoUtil.PICTURE_WIDTH);
//            Bitmap bitmap = PhotoUtil.convertToBitmap2(path);
            if(bitmap != null){
                Log.i(TAG,bitmap.getHeight()+"x"+bitmap.getWidth()+"图");
                show_pic.setImageBitmap(bitmap);
            }

//            Bitmap bitmap2 = PhotoUtil.convertToBitmap(path,500, 500);
//            if(bitmap2 != null){
//                Log.i(TAG,bitmap2.getHeight()+"x"+bitmap2.getWidth()+"图");
//                show_pic.setImageBitmap(bitmap2);
//            }

//			Bundle extras = data.getExtras();
//			if (extras != null) {
//				Bitmap photo = extras.getParcelable("data");
//				ByteArrayOutputStream stream = new ByteArrayOutputStream();
//				photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);// (0-100)压缩文件
//				InputStream isBm = new ByteArrayInputStream(stream.toByteArray());
//			}

        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}
